package org.lanqiao.lg.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.lanqiao.bean.Company;
import org.lanqiao.lg.dao.impl.CompanyDaoImpl;
import org.lanqiao.tools.BaseServlet;

import com.google.gson.Gson;

@WebServlet("/RegisterCom")
public class RegisterCom extends BaseServlet {
	Gson gson = new Gson();
	CompanyDaoImpl comdao = new CompanyDaoImpl();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.request = request;
		this.response = response;

		String key = getString("key");
		System.out.println(request.getSession().getId() + key);
		redata.setMsg("");
		if (!isNull("key")) {
			switch (key) {
			case "registered":
				registeredCom();
				break;

			default:
				redata.setCode(3);
				redata.setMsg("key参数异常");
				redata.setData("");
				break;
			}
		} else {
			redata.setCode(3);
			redata.setMsg("key参数为空");
			redata.setData("");
		}
		System.out.println(redata.toString());
		response.getWriter().write(gson.toJson(redata));
	}

	// 公司注册 状态转到1
	private void registeredCom() {
		String num = getString("b1");
		String pwd = getString("password");
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		Integer i = comdao.insertCompanyNum(new Company(num, pwd));
		if (i > 0) {
			Integer id = comdao.selectCompanyId(new Company(num, pwd));
//			把标识存入Session会话里面
			HttpSession session = request.getSession();
			session.setAttribute("id", id);
			session.setAttribute("typeU", "2");
			System.out.println(request.getSession().getId() + "/id=" + id + "/registered");
			redata.setCode(2);
			redata.setMsg("insertCompanyNum 成功");
			redata.setData(i);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(i);
			return;
		}
	}

}
